
package linuxbox.bbs.bean;

import java.util.*;

import linuxbox.bbs.exception.*;
import linuxbox.util.*;
import linuxbox.bbs.logic.*;

public class BbsMsgHome {
	private	DbAdapter m_db = null;
	private	boolean m_bTransaction = false;
	
	public BbsMsgHome() {
		m_db = null;
		m_bTransaction = false;
	}
	
	public BbsMsgHome(DbAdapter db) {
		m_db = db;
		m_bTransaction = true;
	}

	public void create(BbsMsgKey pk) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(pk, m_db);
		else
			bean = new BbsMsgBean(pk);

		bean.create();

		if (pk.m_lMsgID == 0)
			pk.m_lMsgID = bean.getMsgID();

		if (pk.m_lTopicID == 0)
			pk.m_lTopicID = bean.getTopicID();
	}

	public BbsMsgKey create(BbsMsg msg) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		BbsMsgKey key = bean.create(msg);

		return key;
	}

	public void remove(BbsMsgKey pk) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(pk, m_db);
		else
			bean = new BbsMsgBean(pk);
		bean.remove();
	}

	public BbsMsgKey findByKey(BbsMsgKey pk) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(pk, m_db);
		else
			bean = new BbsMsgBean(pk);
		
		return bean.findByKey();
	}

	public Vector findBySql(String sqlWhere) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		return bean.findBySql(sqlWhere);
	}

	public ArrayList findNewList(int iCount) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
		
		return bean.findNewList(iCount);
	}

	public int getMsgCount(int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getMsgCount(iCatID);
	}		

	public long getTopicCount(int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getTopicCount(iCatID);
	}		

	public long getMaxMsgID(int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getMaxMsgID(iCatID);
	}		

	public long getMaxTopicID(int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getMaxTopicID(iCatID);
	}		

	public long getRequestTopicID(long lNumber, int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getRequestTopicID(lNumber, iCatID);
	}		

	public Vector getNext20(long begin, int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		return bean.getNext20(begin, iCatID);
	}		

	public Vector getMsgListByTopic(long lTopicID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		return bean.getMsgListByTopic(lTopicID);
	}		

	public boolean hasChildMsg(long lTopicID, long lMsgID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		return bean.hasChildMsg(lTopicID, lMsgID);
	}		

	public BbsMsgKey getNextTopic(long lTopicID, int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getNextTopic(lTopicID, iCatID);
	}		

	public BbsMsgKey getPrevTopic(long lTopicID, int iCatID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getPrevTopic(lTopicID, iCatID);
	}		

	public long getFirstMsgID(long lTopicID) throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);
			
		return bean.getFirstMsgID(lTopicID);
	}		

	public ArrayList queryByType(int iCatID, int iType, String sQuery) 
	throws BbsMsgBeanException {
		BbsMsgBean bean = null;

		if (m_bTransaction)
			bean = new BbsMsgBean(null, m_db);
		else
			bean = new BbsMsgBean(null);

		return bean.queryByType(iCatID, iType, sQuery);
	}		
}